from .SumaMain import *
from .SumaHTTP import *
from .SumaFile import *

class SumaLog:
    def __init__(self):
        self.creater_name = "SumaRoder"
        self.creater_qq = "1493813167"
        self.function_name = "SumaLog"
        self.function_desc = "This is a log manager."
        self.function_version = "3.7"
        self.log_db = [
            {
                "group": "GLOBAL",
                "msg": [
                    "sruslib (SumaLog "+self.function_version+", SumaException "+SumaException.function_version+", SumaConsole "+SumaConsole.function_version+", SumaHTTP "+SumaHTTP.function_version+", SumaFile "+SumaFile.function_version+", Python "+pythonVersion+")",
                ]
            },
            {
                "group": "WARN",
                "msg": []
            }
        ]
        self.group_capacity = 128
        for i in range(len(self.log_db)):
            for j in self.log_db[i]["msg"]:
                print("["+SumaConsole.green("Info")+"]"+"[Group="+SumaConsole.yellow(self.log_db[i]["group"])+"] "+j)
    def color_text(self,color,text,trace_id=None):
        color_text = {"yellow": SumaConsole.yellow(text), "red": SumaConsole.red(text), "black": SumaConsole.black(text), "green": SumaConsole.green(text), "ryan": SumaConsole.ryan(text), "blue": SumaConsole.blue(text)}
        if trace_id == None:
            trace_id = str(uuid.uuid4())
        try:
            return color_text[color]
        except Exception as e:
            self.log("",type="error",e=e)
            traceback.print_exc(file=sys.stderr)
            return False
    def printc(self,color,text,trace_id=None):
        color_text = {"yellow": SumaConsole.yellow(text), "red": SumaConsole.red(text), "black": SumaConsole.black(text), "green": SumaConsole.green(text), "ryan": SumaConsole.ryan(text), "blue": SumaConsole.blue(text)}
        if trace_id == None:
            trace_id = str(uuid.uuid4())
        try:
            print(color_text[color])
            return True
        except Exception as e:
            self.log("",type="error",e=e)
            traceback.print_exc(file=sys.stderr)
            return False
    def log(self,text,type="info",e=None,group="GLOBAL",trace_id=None):
        if trace_id == None:
             trace_id = str(uuid.uuid4())
        try:
            if type == "info":
                print("["+SumaConsole.green("Info")+"]"+"[Group="+SumaConsole.yellow(group)+"] "+text)
                self.groupMsgLogIn(text,group=group)
                return True
            elif type == "warning":
                print("["+SumaConsole.yellow("Warn")+"] "+text)
                self.groupMsgLogIn(text,group="WARN")
            elif type == "error":
                print("["+SumaConsole.red("Error")+"]"+"[TraceId="+SumaConsole.yellow(trace_id)+"] 报错如下:")
                SumaException.traceIdLogIn(e.__class__,trace_id)
        except Exception as e:
            print("["+SumaConsole.red("Error")+"]"+"[TraceId="+SumaConsole.yellow(trace_id)+"] 报错如下:")
            SumaException.traceIdLogIn(e.__class__,trace_id)
            traceback.print_exc(file=sys.stderr)
            return False
    def groupMsgLogIn(self,msg,group="GLOBAL"):
        for i in self.log_db:
           if i["group"] == group:
               i["msg"].append(msg)
               if len(i["msg"]) >= self.group_capacity:
                   i["msg"].pop(0)
               return True
        group_ = {
            "group": group,
            "msg": []
        }
        group_["msg"].append(msg)
        self.log_db.append(group_)
SumaLog = SumaLog()